package com.fjwt.gz.mgr.ctrl.stat;

import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.components.mq.model.ExportExcelMQ;
import com.fjwt.gz.components.mq.vender.IMQSender;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.ACTE027StatDTO;
import com.fjwt.gz.mgr.ctrl.CommonCtrl;
import com.fjwt.gz.service.activity.platform.acte027.ACTE027ExportReport;
import com.fjwt.gz.service.impl.OldFansInfoService;
import com.fjwt.gz.service.impl.StatMemberNumService;
import com.fjwt.gz.service.impl.SysExportTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 统计：福利焕活统计
 * </p>
 *
 * @author shiyonghao
 * @since 2025-07-24
 */
@Slf4j
@RestController
@RequestMapping("/api/acte027/stat")
public class ACTE027StatController extends CommonCtrl {

    @Autowired
    private OldFansInfoService oldFansInfoService;
    @Autowired
    private StatMemberNumService statMemberNumService;
    @Autowired
    private IMQSender mqSender;
    @Autowired
    private SysExportTaskService sysExportTaskService;


    /**
     * 福利焕活领取明细分页列表
     */
    @PreAuthorize("hasAnyAuthority('ENT_DATA_REPORT_WELFARE_NEW_TAKE')")
    @GetMapping("/details")
    public ApiRes details() {
        ACTE027StatDTO acte027StatDTO = getObject(ACTE027StatDTO.class);
        return ApiRes.page(oldFansInfoService.getDetails(getIPage(), acte027StatDTO));
    }

    /**
     * 福利焕活会员召回（机构）明细分页列表
     */
    @PreAuthorize("hasAnyAuthority('ENT_DATA_REPORT_WELFARE_NEW_PROXY')")
    @GetMapping("/agent/recall/details")
    public ApiRes agentRecallDetails() {
        ACTE027StatDTO acte027StatDTO = getObject(ACTE027StatDTO.class);
        return ApiRes.page(statMemberNumService.getAgentRecallDetails(getIPage(), acte027StatDTO));
    }

    /**
     * 福利焕活会员召回情况分页列表
     */
    @PreAuthorize("hasAnyAuthority('ENT_DATA_REPORT_WELFARE_NEW_RECALL')")
    @GetMapping("/recall/details")
    public ApiRes recallDetails() {
        ACTE027StatDTO acte027StatDTO = getObject(ACTE027StatDTO.class);
        return ApiRes.page(statMemberNumService.getRecallDetails(getIPage(), acte027StatDTO));
    }

    /**
     * 福利焕活领取明细导出
     */
    @PreAuthorize("hasAnyAuthority('ENT_DATA_REPORT_WELFARE_NEW_TAKE_EXPT')")
    @GetMapping("/details/export")
    public ApiRes detailsExport() {
        ACTE027StatDTO acte027StatDTO = getObject(ACTE027StatDTO.class);
        acte027StatDTO.setExportType(ACTE027ExportReport.DETAIL);
        // 报表名称
        String fileName = "福利焕活领取明细报表";
        // 添加导出任务记录
        Long exportTaskId = sysExportTaskService.addExportTask(getCurrentUser().getSysUser(), fileName, "ACTE027",
                Constants.EXPORT_TASK_ENUM.RESOURCE_TYPE.ACTIVITY, JSONObject.toJSONString(acte027StatDTO));
        // 发送mq
        mqSender.send(ExportExcelMQ.build(exportTaskId));
        return ApiRes.ok();
    }

    /**
     * 福利焕活会员召回（机构）明细导出
     */
    @PreAuthorize("hasAnyAuthority('ENT_DATA_REPORT_WELFARE_NEW_PROXY_EXPT')")
    @GetMapping("/agent/recall/details/export")
    public ApiRes agentRecallDetailsExport() {
        ACTE027StatDTO acte027StatDTO = getObject(ACTE027StatDTO.class);
        acte027StatDTO.setExportType(ACTE027ExportReport.AGENT_RECALL_DETAILS);
        // 报表名称
        String fileName = "福利焕活会员召回（机构）明细报表";
        // 添加导出任务记录
        Long exportTaskId = sysExportTaskService.addExportTask(getCurrentUser().getSysUser(), fileName, "ACTE027",
                Constants.EXPORT_TASK_ENUM.RESOURCE_TYPE.ACTIVITY, JSONObject.toJSONString(acte027StatDTO));
        // 发送mq
        mqSender.send(ExportExcelMQ.build(exportTaskId));
        return ApiRes.ok();
    }

    /**
     * 福利焕活会员召回情况导出
     */
    @PreAuthorize("hasAnyAuthority('ENT_DATA_REPORT_WELFARE_NEW_RECALL_EXPT')")
    @GetMapping("/recall/details/export")
    public ApiRes recallDetailsExport() {
        ACTE027StatDTO acte027StatDTO = getObject(ACTE027StatDTO.class);
        acte027StatDTO.setExportType(ACTE027ExportReport.RECALL_DETAILS);
        // 报表名称
        String fileName = "福利焕活会员召回情况报表";
        // 添加导出任务记录
        Long exportTaskId = sysExportTaskService.addExportTask(getCurrentUser().getSysUser(), fileName, "ACTE027",
                Constants.EXPORT_TASK_ENUM.RESOURCE_TYPE.ACTIVITY, JSONObject.toJSONString(acte027StatDTO));
        // 发送mq
        mqSender.send(ExportExcelMQ.build(exportTaskId));
        return ApiRes.ok();
    }
}

